package tomcatemb;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.ClassLoaderFactory;
import org.apache.catalina.startup.Tomcat;

import com.attilax.core.AsynUtil;

import javax.servlet.ServletException;

/**
 * tomcatemb.EmbeddedTomcatRunner
 * v2 nodep
 * hislog.EmbeddedTomcatRunner
 * http://localhost/cloud/clinewadvice/clinicvaf1insert?lver=22
 * http://localhost:1314/webcon/commServletV2?class=com.attilax.io.SpaceCheck&method=spaceinfo&paramtypes=&ar
 * Created by nil on 2014/8/1.
 */
public class EmbeddedTomcatRunner {
    private Tomcat tomcat;    
    static int port = 1314;
    static String contextPath = "/webcon";
    static  String baseDir;// =pathx.webAppPath_jensyegeor();

    private void startTomcat(int port, String contextPath, String baseDir)
            throws ServletException, LifecycleException {
        tomcat = new Tomcat();
        tomcat.setPort(port);
        tomcat.setBaseDir(".");
        StandardServer server = (StandardServer) tomcat.getServer();
        //apr is just for static file perf enhance
//        AprLifecycleListener listener = new AprLifecycleListener();
//        server.addLifecycleListener(listener);
        tomcat.addWebapp(contextPath, baseDir);
        tomcat.start();
    }

    private void stopTomcat() throws LifecycleException {
        tomcat.stop();
    }

    public static void main(String[] args) {
     
    	port=Integer.parseInt(args[0]);    	   
    	contextPath= (args[1]);
    	contextPath=contextPath.substring(1);
    	baseDir=args[2];
//    	AsynUtil.execMeth_Ays(new Runnable() {
//			
//			@Override
//			public void run() {
//				 try {
//					ftpapache.main(null);
//				} catch (FtpException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//					
//				}
//				
//			}
//		}, "threadName");
    //	org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher
    //	org.jboss.resteasy.plugins.providers.multipart.MimeMultipartProvider
    	//String base=pathx.webAppPath_jensyegeor();
    	System.out.println("");
        runWzCallback();
    }
    
    public static void runWzCallback(int port,String contextPath,String baseDir,Runnable run) {
		try {
			contextPath=contextPath.substring(1);
         
            System.out.println("basedir:"+baseDir);
            		//new File("webroot").getAbsolutePath();  // 椤圭洰涓瓀eb鐩綍鍚嶇О锛屼互鍓嶇増鏈负WebRoot銆亀ebapp銆亀ebapps锛岀幇鍦ㄤ负WebContent
            EmbeddedTomcatRunner EmbeddedTomcat1 = new EmbeddedTomcatRunner();
            EmbeddedTomcat1.startTomcat(port, contextPath, baseDir);
            // 鐢变簬Tomcat鐨剆tart鏂规硶涓洪潪闃诲鏂规硶,鍔犱竴涓嚎绋嬬潯鐪犳ā鎷熺嚎绋嬮樆锟�
            
            
            String heartbeat_str = "--tomcat run. heartbeat_str";
            
            AsynUtil.execMeth_Ays(run, "threadName");
           Global.heartbeatRecycle(heartbeat_str);
           
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
	}

	private static void runWzCallback() {
		try {
           
         
            System.out.println("basedir:"+baseDir);
            		//new File("webroot").getAbsolutePath();  // 椤圭洰涓瓀eb鐩綍鍚嶇О锛屼互鍓嶇増鏈负WebRoot銆亀ebapp銆亀ebapps锛岀幇鍦ㄤ负WebContent
            EmbeddedTomcatRunner EmbeddedTomcat1 = new EmbeddedTomcatRunner();
            EmbeddedTomcat1.startTomcat(port, contextPath, baseDir);
            // 鐢变簬Tomcat鐨剆tart鏂规硶涓洪潪闃诲鏂规硶,鍔犱竴涓嚎绋嬬潯鐪犳ā鎷熺嚎绋嬮樆锟�
            
            
            String heartbeat_str = "--tomcat run. heartbeat_str";
           Global.heartbeatRecycle(heartbeat_str);
           
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
	}
}